package com.example.zlw.skindemo;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by ZLW on 2017/7/20.
 */

public class SkinManager {
    private Context context;

    private String skinPackage;

    private Resources skinResources;

    private static final SkinManager outInstance = new SkinManager();
    private String defPackage;

    public static SkinManager getOutInstance() {

        return outInstance;
    }

    private SkinManager() {
    }

    public void init(Context context) {
        this.context = context.getApplicationContext();

    }

    public void loadSkin(String path) {
        PackageManager packageManager = context.getPackageManager();
        defPackage = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES).packageName;
//        defPackage = context.getPackageName();

        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
            addAssetPath.setAccessible(true);
            addAssetPath.invoke(assetManager, path);

            //拿到对应插件apk的Resource,就可以拿到里面的资源了
            skinResources = new Resources(assetManager, context.getResources().getDisplayMetrics()
                    , context.getResources().getConfiguration());


        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    /**
     *
     * @param resId 本APP的属性的id
     * @return
     */
    public int getColor(int resId) {
        if (skinResources == null) {
            return resId;
        }

        String resName = context.getResources().getResourceEntryName(resId);

        int skinId = skinResources.getIdentifier(resName,"color",defPackage);

        if (skinId == 0) {
            return resId;
        }
        return skinResources.getColor(skinId);

    }

    /**
     *
     * @param resId 本APP的属性的id
     * @return
     */
    public Drawable getDrawable(int resId) {
        Drawable originDrawable = ContextCompat.getDrawable(context, resId);

        if (skinResources == null) {
            return originDrawable;
        }

//        String resName = skinResources.getResourceName(resId);
        String resName = context.getResources().getResourceEntryName(resId);

        int skinId = skinResources.getIdentifier(resName,"drawable",defPackage);
//        if (skinId == 0) {
//            return originDrawable;
//        }
        Drawable trueDrawable = skinResources.getDrawable(skinId);
        return trueDrawable;

    }

}
